package com.dearbinge.opentapi.content.thumbsCommission;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.thumbsCommission.ThumbsCommissionService;
import com.dearbinge.data.pojo.thumbsCommission.ThumbsCommission;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Api(description = "点赞佣金")
@RestController
@RequestMapping("/thumbsCommission")
@EnableAutoConfiguration
public class ThumbsCommissionController extends WebMvcConfigurerAdapter {

    @Autowired
    private ThumbsCommissionService thumbsCommissionService;

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody ThumbsCommission thumbsCommission){
        try {
            if (thumbsCommission.getId() != null && thumbsCommission.getId() != 0){
                Integer dataCode = thumbsCommissionService.update(thumbsCommission);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else {
                Integer dataCode = thumbsCommissionService.insert(thumbsCommission);
                if (dataCode > 0){
                    return ResultMsg.ok("增加成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除", notes = "根据ID删除点赞佣金信息")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id) {
        try {
            Integer dataCode = thumbsCommissionService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 查询用户的点赞佣金
     */
    @ApiOperation(value = "查询用户的点赞佣金", notes = "查询用户的点赞佣金")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", paramType = "query", required = true),
            @ApiImplicitParam(name = "current", value = "current", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "size", paramType = "query")
    })
    @RequestMapping(value = "/listThumbsCommissionByUserId", method = RequestMethod.GET)
    public ResultMsg<Page> listThumbsCommissionByUserId(String userId, Integer current, Integer size) {
        try {
            Page page = thumbsCommissionService.listThumbsCommissionByUserId(userId, current, size);
            return ResultMsg.okData(page, ResultStatusCodeEnum.SUCCESS.getDesc());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }
}
